package com.papper.mng.controller.sys;

import com.papper.common.bean.sys.SysModel;
import com.papper.common.bean.sys.SysSystem;
import com.papper.common.enums.DataStatusEnum;
import com.papper.common.exception.ServiceException;
import com.papper.common.service.sys.SysModelService;
import com.papper.common.service.sys.SysResourceService;
import com.papper.common.service.sys.SysSystemService;
import com.papper.mng.controller.BaseAction;
import com.papper.mng.domain.DwzResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/sys/model")
public class SysModelAction extends BaseAction {
    @Autowired
    SysSystemService   sysSystemService;
    @Autowired
    SysModelService    sysModelService;
    @Autowired
    SysResourceService sysResourceService;

    /**
     * 系统模块首页
     *
     * @return
     */
    @GetMapping(value = "")
    public String execute(HttpServletRequest _request, Model model, Long systemId) {
        systemId = systemId == null ? 1 : systemId;
        SysSystem sysSystem = sysSystemService.findById(systemId);
        model.addAttribute("sysSystem", sysSystem);
        List<SysSystem> systems = sysSystemService.findAll();
        model.addAttribute("systems", systems);
        List<SysModel> sysModels = sysModelService.findBySystemId(systemId);
        model.addAttribute("sysModels", sysModels);
        return render("sys/model/index");
    }

    /**
     * 系统模块_新增
     *
     * @param _response
     * @throws IOException
     */
    @GetMapping(value = "/add")
    public String add_get(HttpServletResponse _response, Model model, Long systemId) throws IOException {
        model.addAttribute("systemId", systemId);
        SysSystem sysSystem = sysSystemService.findById(systemId);
        model.addAttribute("sysSystem", sysSystem);
        return render("sys/model/add");
    }

    @PostMapping(value = "/add")
    @ResponseBody
    public DwzResponse add_post(HttpServletRequest _request, HttpServletResponse _response, SysModel sysModel) throws IOException {
        try {
            checkSuperCert(_request);
        } catch (ServiceException e) {
            return new DwzResponse("300", e.getMessage());
        }
        if (sysModel == null) {
            return new DwzResponse("300", "参数异常！");
        }
        sysModel.setDataStatus(DataStatusEnum.OPEN.getCode());
        sysModel.setCreateTime(new Date());
        sysModel.setModifyTime(new Date());
        try {
            sysModelService.save(sysModel);
            return new DwzResponse("200", "系统模块新增成功!!!");
        } catch (Exception e) {
            e.printStackTrace();
            return new DwzResponse("300", "系统错误,请稍候再试!!![" + e.getMessage() + "]");
        }
    }

    /**
     * 系统模块_更新
     *
     * @param _response
     * @throws IOException
     */
    @GetMapping(value = "/edit")
    public String edit_get(HttpServletResponse _response, Model model, Long id) throws IOException {
        SysModel sysModel = sysModelService.findById(id);
        model.addAttribute("entity", sysModel);
        SysSystem sysSystem = sysSystemService.findById(sysModel.getSystemId());
        model.addAttribute("sysSystem", sysSystem);
        return render("sys/model/edit");
    }

    @PostMapping(value = "/edit")
    @ResponseBody
    public DwzResponse edit_post(HttpServletRequest _request, HttpServletResponse _response, SysModel sysModel) throws IOException {
        try {
            checkSuperCert(_request);
        } catch (ServiceException e) {
            return new DwzResponse("300", e.getMessage());
        }
        if (sysModel == null || sysModel.getId() == null) {
            return new DwzResponse("300", "参数异常！");
        }
        sysModel.setModifyTime(new Date());
        try {
            sysModelService.update(sysModel);
            return new DwzResponse("200", "系统模块修改成功!!!");
        } catch (Exception e) {
            e.printStackTrace();
            return new DwzResponse("300", "系统错误,请稍候再试!!![" + e.getMessage() + "]");
        }
    }

    /**
     * 系统模块_删除
     *
     * @param _response
     * @param id
     * @throws IOException
     */
    @PostMapping(value = "/del")
    @ResponseBody
    public DwzResponse del(HttpServletRequest _request, HttpServletResponse _response, Long id) throws IOException {
        try {
            checkSuperCert(_request);
        } catch (ServiceException e) {
            return new DwzResponse("300", e.getMessage());
        }
        if (id == null || id.intValue() <= 0) {
            return new DwzResponse("300", "参数异常！");
        }
        // 删除系统
        try {
            sysModelService.deleteById(id);
            sysResourceService.deleteByModelId(id);
            return new DwzResponse("200", "系统删除成功!!!");
        } catch (Exception e) {
            e.printStackTrace();
            return new DwzResponse("300", "系统错误,请稍候再试!!![" + e.getMessage() + "]");
        }
    }

    /**
     * 模块_更新状态
     *
     * @param id
     * @throws IOException
     */
    @PostMapping(value = "/status")
    @ResponseBody
    public DwzResponse status_post(HttpServletRequest _request, Long id, Integer status) {
        try {
            checkVerifyCert(_request);
        } catch (ServiceException e) {
            return new DwzResponse("300", e.getMessage());
        }
        if (id == null || id.intValue() <= 0) {
            return new DwzResponse("300", "参数[id]为空！");
        }
        SysModel old = sysModelService.findById(id);
        if (old == null) {
            return new DwzResponse("300", "参数[id]信息异常！");
        }
        // 模块信息状态变更
        SysModel modify = new SysModel();
        modify.setId(old.getId());
        modify.setSystemId(old.getSystemId());
        modify.setModelCode(old.getModelCode());
        modify.setDataStatus(status);
        modify.setModifyTime(new Date());
        sysModelService.update(modify);
        return new DwzResponse("200", "模块信息变更成功!!!");
    }
}